package com.hotel.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hotel.user.pojo.User;
import com.hotel.user.service.UserService;
import com.jt.common.utils.CookieUtils;
import com.jt.common.vo.SysResult;

@RestController
@RequestMapping("/user/manage")
public class UserController {
  @Autowired
  private UserService userService =null;
  //用户名的存在校验
  @RequestMapping("checkUserName")
	public Map<Object,Object> checkUserName(String userName){
	  	Map<Object ,Object> map = new HashMap<Object,Object>();
	  	try{
	  		Boolean exist=userService.checkUserName(userName);
	  		//判断返回的sysResult值
	  		if(exist){
	  			map.put("success", true);
	  			map.put("message", "该用户名已被注册");
	  		}else{//可用
	  			map.put("success", true);
	  			map.put("message", "恭喜，用户名可用");
	  		}
	  	}catch(Exception e){
	  		e.printStackTrace();
	  		map.put("success", false);
	  		map.put("message","注册失败" );
	  	}
		return map;
	}
//注册表单提交
	@RequestMapping("regist")
	public Map<Object,Object> saveUser(User user){
		Map<Object ,Object> map = new HashMap<Object,Object>();
		try{
			userService.saveUser(user);//业务层封装数据完整
			map.put("success", true);
		}catch(Exception e){
			e.printStackTrace();
			map.put("success", false);
		}
		return map;
	}
	//登录校验
		@RequestMapping("login")
		public  Map<Object,Object> doLogin(User user,HttpServletRequest
				req,HttpServletResponse res){
			//在业务层中实现登录逻辑,存储redis过程
			//返回给控制层一个生成的redis存储ticket值
			//登录成功,ticket是非空,登录失败返回空null/""
			Map<Object ,Object> map = new HashMap<Object,Object>();
			try{

               String ticket=userService.doLogin(user);
			 if(StringUtils.isNotEmpty(ticket)){
				//ticket非空,说明登录成功
				//cookie携带这个ticket返回浏览Cookie的工具类
				CookieUtils.setCookie(req, res, 
						"EM_TICKET", ticket);
				map.put("success", true);
	  			map.put("message", "登陆成功");
			 }else{
				map.put("success", false);
	  			map.put("message", "登陆失败");
			 }
			 }catch(Exception e){
				e.printStackTrace();
				map.put("success", false);
			 }
			   return map;
		 }
		
		//通过js发送的参数ticket获取userJson封装返回给ajax使用
		@RequestMapping("/cookie/{ticket}")
		public Map<Object,Object> checkLoginUserData(@PathVariable
				String ticket){
			Map<Object,Object> map = new HashMap<Object,Object>();
			String userJson=
				userService.checkLoginUserData(ticket);
			if(StringUtils.isNotEmpty(userJson)){
				map.put("success", true);
				map.put("data",userJson);
			}else{
				map.put("success", false);
				map.put("message", "尚未登录");
			}
			return map;
		}
		
		/**
		 * 登出功能   
		 * @return
		 */
		@RequestMapping("/logout")
		@ResponseBody
		public SysResult logout(HttpServletRequest request,HttpServletResponse response){
			try{
				CookieUtils.setCookie(request, response, "EM_TICKET", "", 0);
				return SysResult.ok();
			}catch(Exception e ){
				e.printStackTrace();
				return SysResult.build(201, "登出失败", null);
			}
		}
		
		/**
		 * 后台用户系统
		 */
		//查询用户
		@RequestMapping("query1")
		public User queryUser(String id){
			return userService.queryUser(id);
		}
		//添加用户
		@RequestMapping("/save")
		public String addUser(User user) {
			userService.addUser(user);
			return "sucess";
		}
		
		//删除用户
		@RequestMapping("/delete")
		public String deleteUser(String id) {
			userService.deleteUser(id);
			return "sucess";
		}
		//修改用户信息
		@RequestMapping("update")
		public String updateUser(User user){
			userService.updateUser(user);
			return "sucess";
		}
		
		//查询全部用户
		@RequestMapping("query")
		public List<User> queryUsers(){
			return userService.queryUsers();
		}
		
	//order调用
	//String url="http://userservice/user/manage/findUserByUserName?userName="+userName;
		@RequestMapping("findUserByUserName")
		public User findUserByUserName(String userName){
			try{
				return userService.findUserByUserName(userName);
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
		}
	
}
		
